#include "logview.h"
#include <QFile>
#include <QTextStream>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDebug>

LogViewer::LogViewer(QWidget *parent) : QDialog(parent) {
    setWindowTitle("日志");

    textEdit = new QTextEdit(this);
    textEdit->setReadOnly(true);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(textEdit);

    QPushButton *closeButton = new QPushButton("关闭", this);
    connect(closeButton, &QPushButton::clicked, this, &QDialog::close);
    layout->addWidget(closeButton);

    setLayout(layout);
    resize(1600, 1200);

    // 默认加载log.txt文件
    loadLogFile("log.txt");
}

void LogViewer::loadLogFile(const QString &fileName) {
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "Failed to open" << fileName << "for reading:" << file.errorString();
        // 可选择在此处添加错误处理逻辑，例如显示错误消息框
        return;
    }

    QTextStream in(&file);
    QString content = in.readAll();
    file.close();

    textEdit->setPlainText(content);
}
